﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Serviio.API
{
    public class ServiioMetadata
    {
        private const string endpoint = "/rest/metadata";
        private ServiioConnection conn = null;

        public bool AudioLocalArtExtractorEnabled { get; set; }
        public bool VideoLocalArtExtractorEnabled { get; set; }
        public bool VideoOnlineArtExtractorEnabled { get; set; }
        public bool VideoGenerateLocalThumbnailEnabled { get; set; }
        public string MetadataLanguage { get; set; }
        public bool RetrieveOriginalTitle { get; set; }
        public string DescriptiveMetadataExtractor { get; set; }

        internal ServiioMetadata(ServiioConnection connection)
        {
            this.conn = connection;
            Refresh();
        }

        public bool Refresh()
        {
            try
            {
                XmlDocument xDoc = RESTHelper.Get(conn.Host + endpoint);

                AudioLocalArtExtractorEnabled = Boolean.Parse(xDoc.SelectSingleNode("/metadata/audioLocalArtExtractorEnabled").InnerText);
                VideoLocalArtExtractorEnabled = Boolean.Parse(xDoc.SelectSingleNode("/metadata/videoLocalArtExtractorEnabled").InnerText);
                VideoOnlineArtExtractorEnabled = Boolean.Parse(xDoc.SelectSingleNode("/metadata/videoOnlineArtExtractorEnabled").InnerText);
                VideoGenerateLocalThumbnailEnabled = Boolean.Parse(xDoc.SelectSingleNode("/metadata/videoGenerateLocalThumbnailEnabled").InnerText);
                MetadataLanguage = xDoc.SelectSingleNode("/metadata/metadataLanguage").InnerText;
                RetrieveOriginalTitle = Boolean.Parse(xDoc.SelectSingleNode("/metadata/retrieveOriginalTitle").InnerText);
                DescriptiveMetadataExtractor = xDoc.SelectSingleNode("/metadata/descriptiveMetadataExtractor").InnerText;
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool Update()
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                XmlDeclaration xDec = xDoc.CreateXmlDeclaration("1.0", null, null);
                xDoc.AppendChild(xDec);
                XmlElement xRoot = xDoc.CreateElement("metadata");
                xDoc.AppendChild(xRoot);

                XmlElement xAudioLocalArtExtractorEnabled = xDoc.CreateElement("audioLocalArtExtractorEnabled");
                xAudioLocalArtExtractorEnabled.InnerText = AudioLocalArtExtractorEnabled.ToString();
                xRoot.AppendChild(xAudioLocalArtExtractorEnabled);

                XmlElement xVideoLocalArtExtractorEnabled = xDoc.CreateElement("videoLocalArtExtractorEnabled");
                xVideoLocalArtExtractorEnabled.InnerText = VideoLocalArtExtractorEnabled.ToString();
                xRoot.AppendChild(xVideoLocalArtExtractorEnabled);

                XmlElement xVideoOnlineArtExtractorEnabled = xDoc.CreateElement("videoOnlineArtExtractorEnabled");
                xVideoOnlineArtExtractorEnabled.InnerText = VideoOnlineArtExtractorEnabled.ToString();
                xRoot.AppendChild(xVideoOnlineArtExtractorEnabled);

                XmlElement xVideoGenerateLocalThumbnailEnabled = xDoc.CreateElement("videoGenerateLocalThumbnailEnabled");
                xVideoGenerateLocalThumbnailEnabled.InnerText = VideoGenerateLocalThumbnailEnabled.ToString();
                xRoot.AppendChild(xVideoGenerateLocalThumbnailEnabled);

                XmlElement xMetadataLanguage = xDoc.CreateElement("metadataLanguage");
                xMetadataLanguage.InnerText = MetadataLanguage;
                xRoot.AppendChild(xMetadataLanguage);

                XmlElement xRetrieveOriginalTitle = xDoc.CreateElement("retrieveOriginalTitle");
                xRetrieveOriginalTitle.InnerText = RetrieveOriginalTitle.ToString();
                xRoot.AppendChild(xRetrieveOriginalTitle);

                XmlElement xDescriptiveMetadataExtractor = xDoc.CreateElement("descriptiveMetadataExtractor");
                xDescriptiveMetadataExtractor.InnerText = DescriptiveMetadataExtractor.ToString();
                xRoot.AppendChild(xDescriptiveMetadataExtractor);

                return RESTHelper.Put(conn.Host + endpoint, xDoc);
            }
            catch
            {
                return false;
            }
        }
    }
}
